package com.lzx.applib.utils;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class PreferenceUtil {
    private static final String TAG = "PreferenceUtil";

	private static boolean init = false;
	public static void init(Context context) {
		instance = new PreferenceUtil(context);
        init = true;
	}

	private static PreferenceUtil instance;

	public static PreferenceUtil getInstance() {
		if (!init) throw new IllegalStateException("Must be init first.");
		return instance;
	}

    /**This will not delete when user logout*/
    private SharedPreferences appPreference;
    /**This can be delete when user logout*/
    private SharedPreferences userPreference;
    private PreferenceUtil(Context context) {
		appPreference = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
		userPreference = context.getSharedPreferences("user", Context.MODE_PRIVATE);
	}

    public PreferenceUtil saveUserData(String key, Object value) {
        LogUtil.d(TAG, "saveUserData() called with:", key, value);
        set(userPreference, key, value);
        return this;
    }

    public PreferenceUtil saveAppData(String key, Object value) {
        LogUtil.d(TAG, "saveAppData() called with:", key, value);
        set(appPreference, key, value);
        return this;
    }

    private void set(SharedPreferences preference, String key, Object value) {
        Editor editor = preference.edit();
        if (value == null) {
            editor.remove(key);
        } else if (value instanceof Integer) {
            editor.putInt(key, (Integer) value);
        } else if (value instanceof Boolean) {
            editor.putBoolean(key, (Boolean) value);
        } else if (value instanceof Long) {
            editor.putLong(key, (Long) value);
        } else if (value instanceof Float) {
            editor.putFloat(key, (Float) value);
        } else if (value instanceof String) {
            editor.putString(key, (String) value);
        } else if (value instanceof Set) {
            editor.putStringSet(key, (Set<String>) value);
        } else {
            editor.putString(key, value.toString());
        }
        editor.commit();
    }

    public <T> T  getUserData(String key, T... defaultValue) {
        LogUtil.d(TAG, "getUserData() called with:", key);
        if (defaultValue == null || defaultValue.length == 0) return get(userPreference, key, null);
        return get(userPreference, key, defaultValue[0]);
    }

    public <T> T  getAppData(String key, T... defaultValue) {
        LogUtil.d(TAG, "getAppData() called with:", key);
        if (defaultValue == null || defaultValue.length == 0) return get(appPreference, key, null);
        return get(appPreference, key, defaultValue[0]);
    }

    private <T> T get(SharedPreferences preference, String key, T defaultValue) {
		Map map = preference.getAll();
		T t = (T) map.get(key);
		if (t == null)
			t = defaultValue;

        if (t != null && t instanceof Set) {
            t = (T) new HashSet((Set)t);
        }
		return t;
	}

    public void removeUserData(String... keys) {
        LogUtil.i(TAG, "removeUserData");
        remove(userPreference, keys);
    }

    public void clearUserData() {
        LogUtil.i(TAG, "clearUserData");
        userPreference.edit().clear().commit();
    }

    public void clearAppData(String... keys) {
        LogUtil.i(TAG, "clearAppData");
        remove(appPreference, keys);
    }

	private void remove(SharedPreferences preference, String... keys) {
		Editor editor = preference.edit();
        for (String key : keys) {
		    editor.remove(key);
        }
        editor.commit();
	}


}
